package com.zwj.se.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

/**
 * 字节流
 *
 * @author 摸鱼二长老
 * @version 1.0
 * @createTime 2023/6/14 21:32
 */
public class IoTest {
    /**
     * 字节流写数据
     *
     * @param file 文件
     * @throws IOException 异常
     */
    public static void write(File file) throws IOException {
        String str = "松下问童子，言师采药去。只在此山中，云深不知处。";
        //创建字节输出流
        OutputStream fileOutputStream = Files.newOutputStream(file.toPath());
        //输出字节
        fileOutputStream.write(str.getBytes());
        //关闭资源
        fileOutputStream.close();
    }

    /**
     * 使用字节流读取中文会乱码
     *
     * @param file 文件
     * @return 读取的字符串
     * @throws IOException 异常
     */
    public static String read(File file) throws IOException {
        InputStream fileInputStream = Files.newInputStream(file.toPath());

        byte[] bytes = new byte[1024];
        StringBuilder stringBuilder = new StringBuilder();
        int length;

        while ((length = fileInputStream.read()) != -1) {
            String str = new String(bytes, 0, length);
            stringBuilder.append(str);
        }

        fileInputStream.close();
        return stringBuilder.toString();
    }

    /**
     * 缓冲流写内容
     *
     * @param file 文件
     * @throws IOException 异常
     */
    public static void bufferWrite(File file) throws IOException {
        //缓冲字节输出流
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, true));
        String content = "锄禾日当午，汗滴禾下土；谁知盘中餐，粒粒皆辛苦。";
        //换行
        String newLine = System.getProperty("line.separator");
        bos.write(newLine.getBytes());
        //内容
        bos.write(content.getBytes());
        //关闭资源
        bos.close();
    }

    /**
     * 缓冲字节输入流
     *
     * @param file 文件
     * @return 内容
     * @throws IOException 异常
     */
    public static String bufferRead(File file) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(file.toPath()));
        int length;
        StringBuilder stringBuilder = new StringBuilder();
        byte[] bytes = new byte[1024];
        while ((length = bis.read(bytes)) != -1) {
            stringBuilder.append(new String(bytes, 0, length));
        }
        bis.close();
        return stringBuilder.toString();
    }

    public static void zfWrite(File file) throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8);
        osw.write(System.getProperty("line.separator"));
        osw.write("松下问童子，言师采药去。只在此山中，云深不知处。");
        osw.close();
    }

    public static String zfRead(File file) throws IOException {
        InputStreamReader isr = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
        char[] chars = new char[1024];
        int length;
        StringBuilder stringBuilder = new StringBuilder();

        while ((length = isr.read(chars)) != -1) {
            stringBuilder.append(new String(chars, 0, length));
        }
        isr.close();
        return stringBuilder.toString();
    }


    public static void test() throws IOException {
        String filePath = "/Users/zwj/Documents/code/ideaProject/2023/my-spring-boot/java_se/src/main/resources/account_loan_detail.txt";
        File file = new File(filePath);
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8);
        Long assestId = 1L;
        Long loanApplyId = 202408100001L;

        for (int i = 0; i < 3000; i++) {
            String sql = "INSERT INTO \"SFPNEW\".\"ACCOUNT_LOAN_DETAIL\" (\"ASSET_ID\", \"LOAN_APPLY_ID\", \"PAY_TIME\", \"PAY_AMOUNT\", \"PAY_NUM\", \"PAY_STATUS\", \"RESERVE1\", \"FILE_DATE\", \"CREATE_DATE\") " +
                    "VALUES ('" + assestId + "', '" + loanApplyId + "', TO_DATE('2023-06-01 19:43:21', 'SYYYY-MM-DD HH24:MI:SS'), '3001', '3', '18', '备注', '20230602', NULL);";
            System.out.println(sql);
            osw.write(System.getProperty("line.separator"));
            osw.write(sql);
            assestId = assestId + 1;
            loanApplyId = loanApplyId + 1;
        }
        osw.close();
    }

    public static void test1() throws IOException {
        String filePath = "/Users/zwj/Documents/code/ideaProject/2023/my-spring-boot/java_se/src/main/resources/lx_repay_plan.txt";
        File file = new File(filePath);
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8);
        Long assestId = 1L;
        Long loanApplyId = 202408100001L;

        for (int i = 0; i < 3000; i++) {
            String sql = "INSERT INTO \"SFPNEW\".\"LX_REPAY_PLAN\" (\"SEQ_NO\", \"ASSET_ID\", \"CAPITAL_LOAN_NO\", \"ENCASH_DATE\", \"TERM_NO\", \"PRIN_AMT\", \"INT_AMT\", \"REPAY_PLAN_DATE\", \"READY\", \"FILE_DATE\", \"CREATE_DATE\") VALUES " +
                    "(SEQ_LX_REPAY_PLAN_ID.NEXTVAL, '" + assestId + "', '" + loanApplyId + "', '20250601', '1', '985.15', '45', '20250701', NULL, '20230602', TO_TIMESTAMP('2023-07-13 19:29:09.000000', 'SYYYY-MM-DD HH24:MI:SS:FF6'));";
            System.out.println(sql);
            osw.write(System.getProperty("line.separator"));
            osw.write(sql);
            assestId = assestId + 1;
            loanApplyId = loanApplyId + 1;
        }
        osw.close();
    }

    public static void test2() throws IOException {
        String path = "/Users/zwj/Documents/code/ideaProject/2023/my-spring-boot/java_se/src/main/resources/repayment_20240621.txt";
        File file = new File(path);
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8);
        Long seqNo = 2024061214222200001L;
        for (int i = 0; i < 3500; i++) {
            String content = "|000LA20240610001000016|1|3000.00|1.47|0.00|30|20240621|";
            String txt = seqNo + "" + content;
            osw.write(System.getProperty("line.separator"));
            System.out.println(txt);
            osw.write(txt);
            seqNo = seqNo + 1;
        }
        osw.close();
    }

    public static void main(String[] args) throws IOException {
        String path = "/Users/zwj/Documents/code/ideaProject/2023/my-spring-boot/java_se/src/main/resources/repayment_20240621.txt";
        File file = new File(path);
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8);
        Long seqNo = 2024061214222200001L;
        for (int i = 0; i < 3500; i++) {
            String content = "|000LA20240610001000016|1|3000.00|1.47|0.00|30|20240621|";
            String txt = seqNo + "" + content;
            osw.write(System.getProperty("line.separator"));
            System.out.println(txt);
            osw.write(txt);
            seqNo = seqNo + 1;
        }
        osw.close();
    }
}
